//
//  LeftMVControlCollectionViewDelegateSource.m
//  RongRTCMeeting
//
//  Created by LiuLinhong on 2017/08/18.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "LeftMVControlCollectionViewDelegateSource.h"
#import "MeetingViewController.h"
#import "LeftMVControlCollectionViewItem.h"

@interface LeftMVControlCollectionViewDelegateSource ()

@property (nonatomic, strong) MeetingViewController *meetingViewController;
@property (nonatomic, strong) NSMutableArray *mutableArray;

@end

@implementation LeftMVControlCollectionViewDelegateSource

- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.meetingViewController = (MeetingViewController *) vc;
        self.itemDic = [NSMutableDictionary dictionary];
        self.mutableArray = [NSMutableArray array];
        //正常用户
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speach_request", nil), @"image":@"MeetingMenu0HandsUpGray", @"item":@(0), @"enable":@(0)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_invite", nil), @"image":@"MeetingMenu1InviteGray", @"item":@(1), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_host_meeting", nil), @"image":@"MeetingMenu5UploadGray", @"item":@(2), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardGray", @"item":@(3), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareGray", @"item":@(4), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speaker", nil), @"image":@"MeetingMenu2VideoRecordGray", @"item":@(5), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraGray", @"item":@(6), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteGray", @"item":@(7), @"enable":@(1)}];
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_hungup", nil), @"image":@"MeetingMenu8HungUp", @"item":@(8), @"enable":@(1)}];
    }
    return self;
}

- (void)turnCollectionViewItemToObserver
{
    [self.mutableArray removeAllObjects];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speach_request", nil), @"image":@"MeetingMenu0HandsUpGray", @"item":@(0), @"enable":@(1)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_invite", nil), @"image":@"MeetingMenu1InviteGray", @"item":@(1), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_host_meeting", nil), @"image":@"MeetingMenu5UploadGray", @"item":@(2), @"enable":@(0)}];
    
    if (LarkMeetingManager.is3UseWhiteBoard)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardBlue", @"item":@(3), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardGray", @"item":@(3), @"enable":@(1)}];
    
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareGray", @"item":@(4), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speaker", nil), @"image":@"MeetingMenu2VideoRecordGray", @"item":@(5), @"enable":@(1)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraGray", @"item":@(6), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteGray", @"item":@(7), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_hungup", nil), @"image":@"MeetingMenu8HungUp", @"item":@(8), @"enable":@(1)}];
    [self.meetingViewController.meetingViewBuilder.controlCollectionView reloadData];
}

- (void)turnCollectionViewItemToNormal
{
    [self.mutableArray removeAllObjects];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speach_request", nil), @"image":@"MeetingMenu0HandsUpGray", @"item":@(0), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_invite", nil), @"image":@"MeetingMenu1InviteGray", @"item":@(1), @"enable":@(1)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_host_meeting", nil), @"image":@"MeetingMenu5UploadGray", @"item":@(2), @"enable":@(1)}];
    
    if (LarkMeetingManager.is3UseWhiteBoard)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardBlue", @"item":@(3), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardGray", @"item":@(3), @"enable":@(1)}];
    
    if (LarkMeetingManager.is4ShareScreen)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareBlue", @"item":@(4), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareGray", @"item":@(4), @"enable":@(1)}];
    
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speaker", nil), @"image":@"MeetingMenu2VideoRecordGray", @"item":@(5), @"enable":@(1)}];
    
    if (LarkMeetingManager.isCloseCamera)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraBlue", @"item":@(6), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraGray", @"item":@(6), @"enable":@(1)}];

    if (LarkMeetingManager.is7Mute)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteBlue", @"item":@(7), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteGray", @"item":@(7), @"enable":@(1)}];
    
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_hungup", nil), @"image":@"MeetingMenu8HungUp", @"item":@(8), @"enable":@(1)}];
    [self.meetingViewController.meetingViewBuilder.controlCollectionView reloadData];
}

- (void)turnCollectionViewItemToHost
{
    [self.mutableArray removeAllObjects];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speach_request", nil), @"image":@"MeetingMenu0HandsUpGray", @"item":@(0), @"enable":@(0)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_invite", nil), @"image":@"MeetingMenu1InviteGray", @"item":@(1), @"enable":@(1)}];
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_host_meeting", nil), @"image":@"MeetingMenu5UploadGray", @"item":@(2), @"enable":@(0)}];
    
    if (LarkMeetingManager.is3UseWhiteBoard)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardBlue", @"item":@(3), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_use_white_board", nil), @"image":@"MeetingMenu3WhiteBoardGray", @"item":@(3), @"enable":@(1)}];
    
    if (LarkMeetingManager.is4ShareScreen)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareBlue", @"item":@(4), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_share_screen", nil), @"image":@"MeetingMenu4ShareGray", @"item":@(4), @"enable":@(1)}];
    
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_speaker", nil), @"image":@"MeetingMenu2VideoRecordGray", @"item":@(5), @"enable":@(1)}];
    
    if (LarkMeetingManager.isCloseCamera)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraBlue", @"item":@(6), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_camear", nil), @"image":@"MeetingMenu6CameraGray", @"item":@(6), @"enable":@(1)}];
    
    if (LarkMeetingManager.is7Mute)
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteBlue", @"item":@(7), @"enable":@(2)}];
    else
        [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_mute", nil), @"image":@"MeetingMenu7MuteGray", @"item":@(7), @"enable":@(1)}];
    
    [self.mutableArray addObject:@{@"title":NSLocalizedString(@"meeting_hungup", nil), @"image":@"MeetingMenu8HungUp", @"item":@(8), @"enable":@(1)}];
    [self.meetingViewController.meetingViewBuilder.controlCollectionView reloadData];
}

#pragma mark - NSCollectionViewDataSource
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.mutableArray count];
}

- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView
{
    return 1;
}

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger item = [indexPath item];
    LeftMVControlCollectionViewItem *collectionViewItem = (LeftMVControlCollectionViewItem *)self.itemDic[@(item)];
    
    if (!collectionViewItem)
    {
        collectionViewItem = [collectionView makeItemWithIdentifier:@"LeftMVControlCollectionViewItem" forIndexPath:indexPath];
        [self.itemDic setObject:collectionViewItem forKey:@(item)];
    }
    
    [collectionViewItem reloadCollectionViewItem:(NSDictionary *)self.mutableArray[item]];
    return collectionViewItem;
}

#pragma mark - NSCollectionViewDelegate
- (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
{
    DLog(@"LLH...... didSelectItemsAtIndexPaths: %lu", (unsigned long)collectionView.selectionIndexes.firstIndex);
    NSInteger item = collectionView.selectionIndexes.firstIndex;
    
    if (LarkMeetingManager.observerIndex == RongRTC_User_Observer && (item == 1 || item == 2 || item == 4 || item == 6 || item == 7))
        return;
    
    if (LarkMeetingManager.observerIndex == RongRTC_User_Normal && (item == 0))
        return;
    
    if (LarkMeetingManager.observerIndex == RongRTC_User_Host && (item == 0 || item == 2))
        return;
    
    switch (item)
    {
        case 0: //请求发言
            [self.meetingViewController requestSpeakButtonPressed];
            break;
        case 1: //邀请
            [self.meetingViewController inviteButtonPressed];
            break;
        case 2: //主持会议
            [self.meetingViewController manageMeetingButtonPressed];
            break;
        case 3: //使用白板
            [self.meetingViewController useWhiteBoardButtonPressed];
            break;
        case 4: //共享屏幕
            [self.meetingViewController shareScreenButtonPressed];
            break;
        case 5: //扬声器
            [self.meetingViewController videoRecordButtonPressed];
            break;
        case 6: //切换摄像头
            [self.meetingViewController closeCameraButtonPressed];
            break;
        case 7: //静音
            [self.meetingViewController muteButtonPressed];
            break;
        case 8: //挂断
            [self.meetingViewController hungupButtonPressed];
            break;
        default:
            break;
    }
    
    [collectionView deselectItemsAtIndexPaths:indexPaths];
}

@end
